package com.gs.controller;

import java.util.List;



import javax.annotation.Resource;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gs.pojo.Attribute;
import com.gs.pojo.RoomSet;
import com.gs.service.attribute.AttributeService;
import com.gs.service.roomSet.RoomSetService;
import com.gs.utils.Constants;
import com.gs.utils.Page;

@Controller
@RequestMapping("/RoomSet")
public class RoomSetController {

	@Resource
	private RoomSetService roomSetService;
	
	@Resource
	private AttributeService attributeService;

	//分页和模糊查询
	@RequestMapping("/tolist")
	public String list(Model model,
								String txtname,
								@RequestParam(value="currentPage",required=false,defaultValue="1") Integer currentPage){
		//页面容量
		int pageSize = Constants.pageSize;
		//总数量
		int totalCount =roomSetService.getRoomCount(txtname);
		//总页数
		Page page= new Page();
		page.setCurrentPage(currentPage);
		page.setPageSize(pageSize);
		page.setTotal(totalCount);
		Integer totalPage= (int) page.getTotalPage();

		//控制首页和尾页
		if(currentPage < 1){
			currentPage = 1;
		}else if(currentPage > totalPage){
			currentPage = totalPage;
		}
		
		List<RoomSet> roomList = roomSetService.getRoomList(txtname, currentPage, pageSize);
		model.addAttribute("page", page);
		model.addAttribute("txtname",txtname );
		model.addAttribute("roomList",roomList);
		return "/roomset/roomset";
	}
	
	//验证房间号是否存在
	@RequestMapping(value="/YZ")
	@ResponseBody
	public Object YZ(@Param("roomNumber")String roomNumber){
		int YorN=roomSetService.selectYZ(roomNumber);
		return YorN;
	} 
	
	//去到添加页面
	@RequestMapping(value="/toadd")
	public String toadd(Model model){
		List<Attribute> levelList=attributeService.selectGuestRoomLevel();//客房等级集合
		List<Attribute> statelist=attributeService.selectRoomState();//房态集合
		
		model.addAttribute("levelList", levelList);
		model.addAttribute("statelist",statelist );
		return "/roomset/add";
	}
	
	//添加处理
	@RequestMapping(value="/add")
	public String add(RoomSet roomSet){
		int num=roomSetService.insertAll(roomSet);
		if(num>0){
			return "redirect:/RoomSet/tolist.do";
		}
		return "/roomset/add";
	}
	
	//去到修改页面
	@RequestMapping(value="/toupdate")
	public String toupdate(Integer id,
										   Model model){
		List<Attribute> levelList=attributeService.selectGuestRoomLevel();//客房等级集合
		List<Attribute> statelist=attributeService.selectRoomState();//房态集合
		model.addAttribute("levelList", levelList);
		model.addAttribute("statelist",statelist );
		
		RoomSet listPo=roomSetService.selectById(id);
		model.addAttribute("listPo", listPo);
		return "/roomset/update";
	}
	
	//修改处理
	@RequestMapping(value="/update")
	public String update(RoomSet roomSet){
		//System.out.println(id+"~~~~~");
		int num=roomSetService.updateById(roomSet);
		if(num>0){
			return "redirect:/RoomSet/tolist.do";
		}
			return "/roomset/update";
	}
	
	//删除房间
	@RequestMapping("/delete")
	public String delete(String id){
		String[] FenGe=id.split(",");
		for (int i = 0; i < FenGe.length; i++) {
			roomSetService.deleteById(Integer.parseInt(FenGe[i]));
		}
		return "redirect:/RoomSet/tolist.do";
	}
	
	

}
